package com.smile.chatview;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.smile.interf.RegisterInterface;
import com.smile.presenter.RegisterPresenter;

public class RegisterView extends JFrame implements RegisterInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LoginView loginView;
	private RegisterPanel contentPane;
	private JButton minButton;
	private JButton exitButton;
	private JPanel topPanel;

	/**
	 * 鼠标点击时的坐标
	 */
	private int nowX;
	private int nowY;
	/**
	 * 窗口的坐标
	 */
	private int jfX;
	private int jfY;
	private JTextField usernameTF;
	private JPasswordField passwordJPd;
	private JButton registerButton;
	private JLabel registerLabel;
	private JPanel contentPanel;
	private JPasswordField confirmPwddField;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel error_name;
	private JLabel error_password;
	private JLabel error_repassword;
	private JLabel reConn;

	private RegisterPresenter registerPresenter;

	/**
	 * Create the frame.
	 */
	public RegisterView(LoginView loginView) {
		registerPresenter = new RegisterPresenter(this);
		this.loginView = loginView;
		setUndecorated(true);
		setBackground(new Color(0, 0, 0, 0));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(500, 100, 250, 420);
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		contentPane = new RegisterPanel();
		contentPane.setOpaque(false);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		topPanel = new JPanel();
		topPanel.setBackground(new Color(0, 0, 0, 1));
		topPanel.setBounds(0, 45, 251, 25);
		topPanel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				setLocation(jfX + (e.getXOnScreen() - nowX),
						jfY + (e.getYOnScreen() - nowY));
			}
		});
		topPanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				nowX = e.getXOnScreen();
				nowY = e.getYOnScreen();
				jfX = getX();
				jfY = getY();
			}
		});

		contentPane.add(topPanel);
		topPanel.setLayout(null);

		minButton = new JButton("一");
		minButton.setBounds(191, 0, 30, 25);
		topPanel.add(minButton);
		minButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(JFrame.ICONIFIED);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				minButton.setBackground(new Color(120, 175, 250));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				minButton.setBackground(new Color(236, 236, 236));
			}
		});
		minButton.setMargin(new Insets(0, 0, 0, 0));
		minButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));
		minButton.setUI(new OvalButton(new Color(70, 145, 250), false));
		minButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));

		exitButton = new JButton("X");
		exitButton.setBounds(221, 0, 30, 25);
		topPanel.add(exitButton);
		exitButton.setMargin(new Insets(0, 0, 0, 0));
		exitButton.setUI(new OvalButton(Color.RED, true));
		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				RegisterView.this.loginView.setVisible(true);
				RegisterView.this.dispose();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				exitButton.setBackground(new Color(250, 100, 110));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				exitButton.setBackground(new Color(236, 236, 236));
			}
		});
		contentPane.setLayout(null);

		contentPanel = new JPanel();
		// contentPanel.setBackground(new Color(0, 0, 0, 0));
		contentPanel.setBounds(10, 91, 230, 319);
		contentPane.add(contentPanel);

		JLabel loginLabel = new JLabel("注册");
		loginLabel.setFont(new Font("宋体", Font.BOLD, 22));

		usernameTF = new JTextField();
		usernameTF.setHorizontalAlignment(JTextField.CENTER);
		usernameTF.setColumns(1);
		usernameTF.setUI(new MyTextField());

		passwordJPd = new JPasswordField();
		passwordJPd.setHorizontalAlignment(SwingConstants.CENTER);
		passwordJPd.setColumns(1);
		passwordJPd.setUI(new MyPasswordField());

		registerButton = new JButton("立即注册");
		registerButton.setUI(new MyLoginButton());
		registerButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (registerButton.isEnabled()) {
					error_name.setVisible(false);
					error_password.setVisible(false);
					error_repassword.setVisible(false);
					registerPresenter.register(usernameTF.getText(),
							new String(passwordJPd.getPassword()), new String(
									confirmPwddField.getPassword()));
				}
			}
		});

		registerLabel = new JLabel("用户登录");
		registerLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				RegisterView.this.loginView.setVisible(true);
				RegisterView.this.loginView.setUsername("");
				RegisterView.this.dispose();
			}

			@Override
			public void mousePressed(MouseEvent e) {
				registerLabel.setForeground(Color.RED);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				registerLabel.setForeground(new Color(250, 50, 60));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				registerLabel.setForeground(new Color(250, 50, 60));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				registerLabel.setForeground(Color.BLACK);
			}
		});

		confirmPwddField = new JPasswordField();
		confirmPwddField.setHorizontalAlignment(SwingConstants.CENTER);
		confirmPwddField.setColumns(1);
		confirmPwddField.setUI(new MyPasswordField());

		label = new JLabel("昵称");

		label_1 = new JLabel("密码");

		label_2 = new JLabel("确认密码");

		error_name = new JLabel("请输入昵称");
		error_name.setForeground(Color.RED);
		error_name.setVisible(false);

		error_password = new JLabel("请输入密码");
		error_password.setForeground(Color.RED);
		error_password.setVisible(false);

		error_repassword = new JLabel("两次密码不同");
		error_repassword.setForeground(Color.RED);
		error_repassword.setVisible(false);

		reConn = new JLabel("已连接上服务器");
		reConn.setEnabled(false);
		reConn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (reConn.isEnabled()) {
					reConn.setText("连接服务器中...");
					reConn.setEnabled(false);
					registerPresenter.conn();
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				reConn.setForeground(Color.RED);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				reConn.setForeground(new Color(250, 50, 60));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				reConn.setForeground(new Color(250, 50, 60));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				reConn.setForeground(Color.RED);
			}
		});
		reConn.setHorizontalAlignment(SwingConstants.CENTER);
		reConn.setForeground(Color.RED);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel
				.setHorizontalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								gl_contentPanel
										.createSequentialGroup()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																Alignment.LEADING,
																gl_contentPanel
																		.createSequentialGroup()
																		.addGap(90)
																		.addComponent(
																				loginLabel))
														.addGroup(
																Alignment.LEADING,
																gl_contentPanel
																		.createSequentialGroup()
																		.addGap(26)
																		.addGroup(
																				gl_contentPanel
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								Alignment.TRAILING,
																								gl_contentPanel
																										.createSequentialGroup()
																										.addComponent(
																												label_1)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												92,
																												Short.MAX_VALUE)
																										.addComponent(
																												error_password))
																						.addComponent(
																								passwordJPd,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								176,
																								Short.MAX_VALUE)
																						.addComponent(
																								confirmPwddField,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								176,
																								Short.MAX_VALUE)
																						.addGroup(
																								Alignment.TRAILING,
																								gl_contentPanel
																										.createSequentialGroup()
																										.addComponent(
																												label_2)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												56,
																												Short.MAX_VALUE)
																										.addComponent(
																												error_repassword))
																						.addGroup(
																								Alignment.TRAILING,
																								gl_contentPanel
																										.createSequentialGroup()
																										.addComponent(
																												label)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												92,
																												Short.MAX_VALUE)
																										.addComponent(
																												error_name))
																						.addComponent(
																								usernameTF,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								176,
																								Short.MAX_VALUE)
																						.addComponent(
																								reConn,
																								GroupLayout.DEFAULT_SIZE,
																								176,
																								Short.MAX_VALUE)
																						.addComponent(
																								registerButton,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								176,
																								Short.MAX_VALUE)
																						.addGroup(
																								Alignment.TRAILING,
																								gl_contentPanel
																										.createSequentialGroup()
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												66,
																												GroupLayout.PREFERRED_SIZE)
																										.addComponent(
																												registerLabel)
																										.addGap(62)))))
										.addGap(28)));
		gl_contentPanel
				.setVerticalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(loginLabel)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label)
														.addComponent(
																error_name))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(usernameTF,
												GroupLayout.PREFERRED_SIZE, 34,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_1)
														.addComponent(
																error_password))
										.addPreferredGap(
												ComponentPlacement.RELATED,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(passwordJPd,
												GroupLayout.PREFERRED_SIZE, 34,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_2)
														.addComponent(
																error_repassword))
										.addGap(4)
										.addComponent(confirmPwddField,
												GroupLayout.PREFERRED_SIZE, 34,
												GroupLayout.PREFERRED_SIZE)
										.addGap(2)
										.addComponent(reConn,
												GroupLayout.PREFERRED_SIZE, 20,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(registerButton,
												GroupLayout.PREFERRED_SIZE, 31,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addComponent(registerLabel)
										.addContainerGap()));
		contentPanel.setLayout(gl_contentPanel);

		registerPresenter.conn();
	}

	@Override
	public void setConn(boolean is) {
		if (is) {
			reConn.setText("已连接上服务器");
			reConn.setEnabled(false);
		} else {
			reConn.setEnabled(true);
			reConn.setText("未连接上服务器，点击重连...");
		}
	}

	@Override
	public void setErrorMessage(int which, String message) {
		switch (which) {
		case 1:
			error_name.setVisible(true);
			error_name.setText(message);
			break;
		case 2:
			error_password.setVisible(true);
			error_password.setText(message);
			break;
		case 3:
			error_repassword.setVisible(true);
			error_repassword.setText(message);
			break;
		default:
			break;
		}
	}

	@Override
	public void setRegisterButton(boolean is) {
		if (is) {
			registerButton.setEnabled(true);
			registerButton.setText("立即注册");
		} else {
			registerButton.setEnabled(false);
			registerButton.setText("注册中...");
		}
	}

	@Override
	public void setRegisterMessage(boolean is, String username) {
		if (is) {

			JOptionPane.showMessageDialog(this, "用户名是:" + username + "\n请记住！", "注册成功",
					JOptionPane.PLAIN_MESSAGE);

			RegisterView.this.loginView.setVisible(true);
			RegisterView.this.loginView.setUsername(username);
			RegisterView.this.dispose();
		}
	}

}
